package lib.persistence.commands;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GetAllModels 
	extends CommandAbstract
{
	private Connection conn;
	public GetAllModels()
	{
		this.conn = global.ConnectionManager.getInstance().getConnection();
	}
	public List<Object> execute(String tableName, String columnName)  throws SQLException, IOException, ClassNotFoundException 
	{

		String cmd = String.format("SELECT %s FROM %s",columnName, tableName);
		PreparedStatement statement = conn.prepareStatement(cmd);
		ResultSet rs = statement.executeQuery();
		ArrayList<Object> out = new ArrayList<Object>();
		while(rs.next())
		{
			InputStream is = rs.getBlob(columnName).getBinaryStream();
			ObjectInputStream oip = new ObjectInputStream(is);
			out.add(oip.readObject());
		}
		return out; 	
	}

}
